package com.cbtx.tablet.fragments

import android.net.http.SslError
import android.os.Bundle
import android.view.View
import android.webkit.SslErrorHandler
import android.webkit.WebView
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.cbtx.tablet.R
import com.just.agentweb.AgentWeb
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient

import kotlinx.android.synthetic.main.sp_fragment_vr.*

/**
 * author : joker.peng
 * e-mail : joker@fiture.com
 * date   : 2021/7/5 1:29 PM
 * desc   :
 */
class VrFragment : CommonFragment() {

    var mAgentWeb: AgentWeb? = null
    private val url = "https://vr.txcb.com"

    private val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            if (newProgress == 100) {
                if(ll_progress_tips != null){
                    ll_progress_tips.visibility = View.GONE
                }
            }
        }
    }

    override fun setLayout(): Any {
        return R.layout.sp_fragment_vr
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        initWeb()
    }


    private fun initWeb() {
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(mWebFrameLayout, FrameLayout.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .setWebChromeClient(mWebChromeClient)
            .setWebViewClient(getWebViewClient())
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun onPause() {
        super.onPause()
        mAgentWeb?.webLifeCycle?.onPause()
    }

    override fun onResume() {
        super.onResume()

        mAgentWeb?.webLifeCycle?.onResume()
    }

    private fun getWebViewClient(): WebViewClient {
        return object : WebViewClient() {
            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler,
                error: SslError?
            ) {
                handler.proceed()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mAgentWeb?.urlLoader?.loadUrl("")
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if (mIsLazyLoad) {
            if (isVisibleToUser) {
                mAgentWeb?.urlLoader?.loadUrl(url)

            } else {
                mAgentWeb?.urlLoader?.loadUrl("")
            }
        }
    }


}